package com.feri.findlife.service.impl;

import com.feri.findlife.config.SystemConfig;
import com.feri.findlife.dao.ObjstorageDao;
import com.feri.findlife.pojo.Objstorage;
import com.feri.findlife.service.intf.ObjstorageService;
import com.feri.findlife.third.AliOssUtil;
import com.feri.findlife.util.StringUtil;
import com.feri.findlife.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.nio.ch.ThreadPool;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;

/**
 * @program: FindLife
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2020-12-22 09:50
 */
@Service
public class ObjstorageServiceImpl implements ObjstorageService {
    @Autowired
    private ObjstorageDao dao;
    @Override
    public R uploadV1(MultipartFile file) {
        //1.校验有没有内容
        if(!file.isEmpty()){
            //获取文件名称
            String fname=file.getOriginalFilename();
            //长度和重名处理
            fname=rename(fname);
            try {
                //获取文件内容
                byte[] data=file.getBytes();
                //存储在本地
                FileOutputStream fos=new FileOutputStream("images/"+fname);
                fos.write(data);
                fos.flush();
                fos.close();
                return R.ok();
            } catch (IOException e) {
                e.printStackTrace();
                return R.fail();
            }
        }
        return R.fail("亲，请选择文件内容");
    }

    @Override
    public R uploadV2(MultipartFile file) {
        //1.校验有没有内容
        if(!file.isEmpty()){
            //获取文件名称
            String fname=file.getOriginalFilename();
            //长度和重名处理
            fname=rename(fname);
            try {
                //将文件内容存储到OSS
                String url=AliOssUtil.upload(SystemConfig.OSS_BNAME,fname,file.getBytes());
                if(StringUtil.checkEmpty(url)){
                    Objstorage ob=new Objstorage(SystemConfig.OSS_BNAME,fname,url,1);
                    if(dao.save(ob)>0){
                        return R.ok(ob);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
                return R.fail();
            }
        }
        return R.fail("亲，请选择文件内容");
    }

    @Override
    public R uploadBatch(MultipartFile[] files) {
        ArrayList<Objstorage>list=new ArrayList<>();
        if(files!=null && files.length>0){
            for(MultipartFile file:files){
                //1.校验有没有内容
                if(!file.isEmpty()){
                    //获取文件名称
                    String fname=file.getOriginalFilename();
                    //长度和重名处理
                    fname=rename(fname);
                    try {
                        //将文件内容存储到OSS
                        String url=AliOssUtil.upload(SystemConfig.OSS_BNAME,fname,file.getBytes());
                        if(StringUtil.checkEmpty(url)){
                            Objstorage ob=new Objstorage(SystemConfig.OSS_BNAME,fname,url,1);
                            if(dao.save(ob)>0){
                                list.add(ob);
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();

                    }
                }
            }
        }
        return R.ok(list);
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("GC运行了");
    }

    ThreadLocal<ObjstorageService> local;

    //对上传的文件名的长度和重名进行处理
    private String rename(String f){
        //校验长度是否超过50
        if(f.length()>50){
            f=f.substring(f.length()-50);
        }
        //拼接上唯一码 UUID
        return UUID.randomUUID().toString().replaceAll("-","")+"_"+f;
    }

}
